﻿using ConsoleApp1.controls.src;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;

namespace com.sddemo.uikit.controls
{
    public partial class AFRChart : UserControl
    {
        public AFRChart()
        {
            InitializeComponent();
        }
        #region 对外接口

        /// <summary>
        /// InitCurve:FreqGain/FreqOutput
        /// </summary>
        public void InitCurve(EnumCurveType m_FreqType)
        {
            switch (m_FreqType)
            {
                case EnumCurveType.InputAndGain:
                    InitGainCurve();
                    break;
                case EnumCurveType.InputAndOutput:
                    InitOutputCurve();
                    break;
                default:
                    InitGainCurve();
                    break;
            }
        }
        public void RemoveAllFrequencyGainCurves()
        {
            ctlFreqGainGraph.RemoveAllCurve();
        }
        
        #endregion


        #region Initcurve
        private void InitGainCurve()
        {
            ctlFreqGainGraph.EarFlag = "Right";
            ctlFreqGainGraph.CurveType = AChart.CurveTypeS.Client;
            ctlFreqGainGraph.ClearAxis();
            ctlFreqGainGraph.ClearGraph();

            ctlFreqGainGraph.CurveWidth = 1;
            ctlFreqGainGraph.CurveColor = ColorTranslator.ToWin32(Color.Red);
            //ctlHCurve1.CurveColor = 0;
            ctlFreqGainGraph.YTitle = "(dB)";//"(dB HL)";
            ctlFreqGainGraph.XTitle = "(Hz)";
            ctlFreqGainGraph.IsEdit = true;
            ctlFreqGainGraph.BorderStyle = BorderStyle.None;
            ctlFreqGainGraph.Ynames = "50//40//30//20//10//0//-10//-20//-30";//0//20//40//60//80//100//120"
            ctlFreqGainGraph.YMinValue = -30;
            ctlFreqGainGraph.YMaxValue = 50;
            ctlFreqGainGraph.YPointCount = 16;
            ctlFreqGainGraph.Xnames = "125//250//500//1k//2k//4k//8k"; //"125//250//500//1k//2k//4k//8k";
            ctlFreqGainGraph.XMinValue = 125;
            ctlFreqGainGraph.XMaxValue = 8000;//8000;//12000;//8000;
            ctlFreqGainGraph.XPointCount = 12;//12;
            ctlFreqGainGraph.RegionColor = Color.FromArgb(210, 250, 250);
            ctlFreqGainGraph.MaxGainRegionColor = Color.FromArgb(46, 79, 122);
            ctlFreqGainGraph.IsShowMaxGainRegion = false;
            ctlFreqGainGraph.DrawUpAxis();

            ctlFreqGainGraph.Paint += new System.Windows.Forms.PaintEventHandler(ctlFreqGainGraph_Paint);
        }

        private void InitOutputCurve()
        {
            ctlFreqGainGraph.EarFlag = "Right";
            ctlFreqGainGraph.CurveType = AChart.CurveTypeS.Client;
            ctlFreqGainGraph.ClearAxis();
            ctlFreqGainGraph.ClearGraph();
            ctlFreqGainGraph.CurveWidth = 1;
            ctlFreqGainGraph.CurveColor = ColorTranslator.ToWin32(Color.Red);
            //ctlHCurve1.CurveColor = 0;
            ctlFreqGainGraph.YTitle = "(dB)";//"(dB HL)";
            ctlFreqGainGraph.XTitle = "(Hz)";
            ctlFreqGainGraph.IsEdit = true;
            ctlFreqGainGraph.Ynames = "160//150//140//130//120//110//100//90//80//70//60//50//40//30";//0//20//40//60//80//100//120"
            ctlFreqGainGraph.YMinValue = 30;
            ctlFreqGainGraph.YMaxValue = 160;
            ctlFreqGainGraph.YPointCount = 26;
            ctlFreqGainGraph.BorderStyle = BorderStyle.None;
            //横坐标 8000与12000的设置
            //12000
            ctlFreqGainGraph.Xnames = "125//250//500//1k//2k//4k//8k"; //"125//250//500//1k//2k//4k//8k";
            ctlFreqGainGraph.XMinValue = 125;
            ctlFreqGainGraph.XMaxValue = 8000;//8000;//12000;//8000;
            ctlFreqGainGraph.XPointCount = 12;//12;
            ctlFreqGainGraph.RegionColor = Color.FromArgb(210, 250, 250);
            ctlFreqGainGraph.MaxGainRegionColor = Color.FromArgb(46, 79, 122);
            ctlFreqGainGraph.IsShowMaxGainRegion = false;
            ctlFreqGainGraph.DrawUpAxis();

            ctlFreqGainGraph.Paint += new System.Windows.Forms.PaintEventHandler(ctlFreqGainGraph_Paint);
        }

        private void ctlFreqGainGraph_Paint(object sender, PaintEventArgs e)
        {
            ControlPaint.DrawBorder(e.Graphics, e.ClipRectangle, Color.Red, ButtonBorderStyle.Solid);
        }


        
        public Color[] GetCurveColor()
        {
            var clrCurveColor = new Color[4];
            clrCurveColor[0] = pnl40Color.BackColor;
            clrCurveColor[1] = pnl60Color.BackColor;
            clrCurveColor[2] = pnl80Color.BackColor;
            clrCurveColor[3] = pnl100Color.BackColor;
            return clrCurveColor;
        }
        
        public void SetCurveColor(Color[] clrCurveColor)
        {
            //var clrCurveColor = new Color[4];
            pnl40Color.BackColor = clrCurveColor[0];
            pnl60Color.BackColor = clrCurveColor[1];
            pnl80Color.BackColor = clrCurveColor[2];
            pnl100Color.BackColor = clrCurveColor[3];
        }

        public string[] GetCurveValue()
        {
            var clrCurveValue = new string[4];
            clrCurveValue[0] = lab50.Text;
            clrCurveValue[1] = lab65.Text;
            clrCurveValue[2] = lab80.Text;
            clrCurveValue[3] = lab100.Text;
            return clrCurveValue;
        }
        public void SetCurveValue(string[] clrCurveValue)
        {
            lab50.Text = clrCurveValue[0];
            lab65.Text = clrCurveValue[1];
            lab80.Text = clrCurveValue[2];
            lab100.Text = clrCurveValue[3];
        }
        public void SetCurveValue(int[] clrCurveValue)
        {
            lab50.Text =$"{ clrCurveValue[0]}";
            lab65.Text = $"{ clrCurveValue[1]}";
            lab80.Text = $"{ clrCurveValue[2]}";
            //lab100.Text = $"{ clrCurveValue[3]}";
        }



        public void RefreshChart()
        {
            //画线
            ctlFreqGainGraph.DrawCurves();
            ctlFreqGainGraph.Refresh();
        }
        /// <summary>
        /// AddCurve
        /// </summary>
        /// <param name="austarCurveInfo">Info</param>
        /// <param name="freqCurve">Type</param>
        public void AddCurve(CurveInfo austarCurveInfo, EnumCurveType freqCurve)
        {
            double[] ArrFreqList = austarCurveInfo.AbscissaList;
            double[] ArrDataList = austarCurveInfo.OrdinateList;
            Color CurvesColor = austarCurveInfo.CurveColor;
            int CurveWith = austarCurveInfo.CurveWith;
            string CurveStyle = austarCurveInfo.CurveStyle;

            switch (freqCurve)
            {
                case EnumCurveType.InputAndGain:
                    AddFrequencyGainCurve(ArrFreqList, ArrDataList, CurvesColor, CurveWith, CurveStyle);
                    break;
                case EnumCurveType.InputAndOutput:
                    AddFrequencyGainCurve(ArrFreqList, ArrDataList, CurvesColor, CurveWith, CurveStyle);
                    break;
                case EnumCurveType.Target:
                    AddTargetCurve(ArrFreqList, ArrDataList, CurvesColor, CurveWith, CurveStyle);
                    break;
                default:
                    break;
            }
            
        }
        /// <summary>
        /// AddFrequencyGainCurve
        /// </summary>
        /// <param name="ArrFreqList"></param>
        /// <param name="ArrDataList"></param>
        /// <param name="CurvesColor"></param>
        /// <param name="CurveWith"></param>
        /// <param name="CurveStyle"></param>
        public void AddFrequencyGainCurve(double[] ArrFreqList, double[] ArrDataList, Color CurvesColor,int CurveWith = 2,string CurveStyle = "Solid")
        {
            var CurvesIndex = ctlFreqGainGraph.Curves.Count;
            ctlFreqGainGraph.AddCurve();
            ctlFreqGainGraph.Curves[CurvesIndex].CurveColor = CurvesColor;
            ctlFreqGainGraph.Curves[CurvesIndex].CurveWith = CurveWith;
            ctlFreqGainGraph.Curves[CurvesIndex].PointCount = ArrFreqList.Length;
            ctlFreqGainGraph.Curves[CurvesIndex].CurveStyle = CurveStyle;
            for (int x = 0; x < ArrFreqList.Length; x++)
            {
                ctlFreqGainGraph.Curves[CurvesIndex].AddPoint((int)ArrFreqList[x], ArrDataList[x]);
            }
        }
        /// <summary>
        /// AddCurveTarget
        /// </summary>
        /// <param name="ArrFreqList"></param>
        /// <param name="ArrDataList"></param>
        /// <param name="CurvesColor"></param>
        /// <param name="CurveWith"></param>
        /// <param name="CurveStyle"></param>
        public void AddTargetCurve(double[] ArrFreqList, double[] ArrDataList, Color CurvesColor, int CurveWith = 1, string CurveStyle = "Dot")
        {
            var CurvesIndex = ctlFreqGainGraph.Curves.Count;
            ctlFreqGainGraph.AddCurve();
            ctlFreqGainGraph.Curves[CurvesIndex].CurveColor = CurvesColor;
            ctlFreqGainGraph.Curves[CurvesIndex].CurveWith = CurveWith;
            ctlFreqGainGraph.Curves[CurvesIndex].CurveStyle = CurveStyle;
            ctlFreqGainGraph.Curves[CurvesIndex].PointCount = ArrFreqList.Length;
            for (int i = 0; i < ArrFreqList.Length; i++)
            {
                ctlFreqGainGraph.Curves[CurvesIndex].AddPoint((int)ArrFreqList[i], ArrDataList[i]);
            }
        }


        #endregion

        #region
        public double[] getFequencyList()
        {
            double[] SixthOctaves = new double[]{
                125     ,
                140     ,
                157     ,
                177     ,
                198     ,
                223     ,
                250     ,
                281     ,
                315     ,
                354     ,
                397     ,
                445     ,
                500     ,
                561     ,
                630     ,
                707     ,
                794     ,
                891     ,
                1000    ,
                1122    ,
                1260    ,
                1414    ,
                1587    ,
                1782    ,
                2000    ,
                2245    ,
                2520    ,
                2828    ,
                3175    ,
                3564    ,
                4000    ,
                4490    ,
                5040    ,
                5657    ,
                6350    ,
                7127    ,
                8000    ,
                8980    ,
                10079   ,
                11314 };
            return SixthOctaves;
        }

        public double[] getFequencyValueList(int basicValue = 0)
        {
            var freListLength = getFequencyList().Length;

            double[] SixthOctaves = new double[40];
            Random rd = new Random();　　//无参即为使用系统时钟为种子
            for (int i = 0; i < SixthOctaves.Length; i++)
            {
                SixthOctaves[i] = rd.Next(10, 30) + basicValue;
                Debug.WriteLine($"{i}==>>>{SixthOctaves[i]}");
            }
            return SixthOctaves;
        }
        public double[] getTargetFequencyList(int TargetType = 0)
        {
            double[] TargetFequencyList = null;
            switch (TargetType)
            {
                case 0:
                    TargetFequencyList = new double[] { 250, 500, 750, 1000, 1500, 2000, 3000, 4000, 6000 };
                    break;
                case 1:
                    TargetFequencyList = new double[] { 125, 160, 200, 250, 315, 400, 500, 630, 800, 1000, 1250, 1600, 2000, 2500, 3150, 4000, 5000, 6300, 8000 };
                    break;
                default:
                    break;
            }
            return TargetFequencyList;
        }
        public double[] getTargetFequencyValueList(int TargetType = 0, int basicValue = 0)
        {
            int TargetFequencyCount = 9;
            Random rd = new Random();　　//无参即为使用系统时钟为种子
            switch (TargetType)
            {
                case 0:
                    TargetFequencyCount = 9;
                    break;
                case 1:
                    TargetFequencyCount = 19;
                    break;
                default:
                    break;
            }
            var data = new double[TargetFequencyCount];
            for (int i = 0; i < TargetFequencyCount; i++)
            {
                data[i] = rd.Next(0, 20) + i + basicValue;
                Debug.WriteLine($"{i}==>>>{data[i]}");
            }
            return data;
        }

        public void RefreshFrequencyGainGraph()
        {
            double[] ArrFrequencyList = getFequencyList();
            double[] ArrDataList40 = getFequencyValueList(30);
            double[] ArrDataList60 = getFequencyValueList(40);
            double[] ArrDataList80 = getFequencyValueList(50);
            double[] ArrDataList100 = getFequencyValueList(0);
            var ColorList = GetCurveColor();
            double[] Fig6_Freq_Pro = new double[] { 125, 250, 500, 750, 1000, 1500, 2000, 3000, 4000, 6000, 8000 };
            double[] NAL_OutFreq = new double[] { 125, 160, 200, 250, 315, 400, 500, 630, 800, 1000, 1250, 1600, 2000, 2500, 3150, 4000, 5000, 6300, 8000 };
            double[] ArrTargetFrequencyList = getTargetFequencyList(1);
            double[] ArrTargetFrequencyValueList40 = getTargetFequencyValueList(1, 10);
            double[] ArrTargetFrequencyValueList60 = getTargetFequencyValueList(1, 20);
            double[] ArrTargetFrequencyValueList80 = getTargetFequencyValueList(1, 30);
            RemoveAllFrequencyGainCurves();
            AddFrequencyGainCurve(ArrFrequencyList, ArrDataList40, ColorList[0]);
            AddFrequencyGainCurve(ArrFrequencyList, ArrDataList60, ColorList[1]);
            AddFrequencyGainCurve(ArrFrequencyList, ArrDataList80, ColorList[2]);
            //AddFrequencyGainCurve(ArrFrequencyList, ArrDataList100, ColorList[3]);
            AddTargetCurve(ArrTargetFrequencyList, ArrTargetFrequencyValueList40, Color.DarkOrange);
            AddTargetCurve(ArrTargetFrequencyList, ArrTargetFrequencyValueList60, Color.DarkOrange);
            AddTargetCurve(ArrTargetFrequencyList, ArrTargetFrequencyValueList80, Color.DarkOrange);
            RefreshChart();
        }
        #endregion
    }
}
